{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 拆解可迭代对象\n",
    "##### 1. 可迭代对象长度已知"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ACME 50 91.1 (2012, 12, 21)\n"
     ]
    }
   ],
   "source": [
    "data = ['ACME', 50, 91.1, (2012, 12, 21)]\n",
    "name, shares, price, date = data\n",
    "print(name, shares, price, date)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "source": [
    "##### 2. 丢弃某个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAVIA (2012, 12, 21)\n",
      "-91.1\n"
     ]
    }
   ],
   "source": [
    "data = ['NAVIA', -1, -91.1, (2012, 12, 21)]\n",
    "name, _, _, date = data\n",
    "print(name, date)\n",
    "print(_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 3. 可迭代对象长度未知（*号拆解）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "81.6\n",
      "foo 1 2\n",
      "bar hello\n",
      "foo 3 4\n"
     ]
    }
   ],
   "source": [
    "# 去头去尾取平均值\n",
    "def drop_first_last(grades):\n",
    "    first, *middle, last = grades\n",
    "    return sum(middle)/len(middle)\n",
    "grades = [99, 96, 96, 86, 70, 60, 40]\n",
    "print(drop_first_last(grades))\n",
    "\n",
    "# 处理一个带标记的元组序列\n",
    "records = [\n",
    "    ('foo', 1, 2),\n",
    "    ('bar', 'hello'),\n",
    "    ('foo', 3, 4),\n",
    "]\n",
    "def do_foo(x, y):\n",
    "    print('foo', x, y)\n",
    "\n",
    "def do_bar(s):\n",
    "    print('bar', s)\n",
    "\n",
    "for tag, *args in records:\n",
    "    if tag == 'foo':\n",
    "        do_foo(*args)\n",
    "    elif tag == 'bar':\n",
    "        do_bar(*args)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 4. \"_\"和'*'配合使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ACME 2012\n"
     ]
    }
   ],
   "source": [
    "record = (\"ACME\", 50, 123.45, (12, 18, 2012))\n",
    "name, *_, (*_, year) = record\n",
    "print(name, year)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.4 64-bit",
   "language": "python",
   "name": "python37464bit0eab7a6caf29454899b876d50238c56e"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
